#ifndef HEAD_H
#define HEAD_H
#include<iostream>
using namespace std;

struct node{
    node* nx;
    int data;
};

class arraylist{
private:
    node*head;
    node*tail;
    node*current;
    int length=0;

public:
    arraylist();
    void length_input();
    void creatlist(int i);
    void showlist();
    void findmax();
    void executefindmax(node *p);
    void nodenumber();
    void executenodenumber(node *p);
    void getsum();
    void executegetsum(node*p);
    ~arraylist();
    int max;
    int nodecounter;
    int sum;
};
//04(03
void arraylist::getsum(){
    sum=0;
    node*p=head->nx;
    executegetsum(p);
}

void arraylist::executegetsum(node *p){
    if(p!=NULL)
        sum+=p->data;
    if(p->nx!=NULL)
        executegetsum(p->nx);
}


//04(02
void arraylist::nodenumber(){
    node*p=head->nx;
    nodecounter=0;
    executenodenumber(p);
}

void arraylist::executenodenumber(node*p){
    if(p!=NULL)
        nodecounter++;
    if(p->nx!=NULL)
        executenodenumber(p->nx);
}

//04(01
void arraylist::findmax(){
    node*p=head->nx;
    max=p->data;
    executefindmax(p);
}

void arraylist::executefindmax(node *p){
    if(p->data>max){
        max=p->data;
    }

    if(p->nx!=NULL){
        executefindmax(p->nx);
    }
}

//int arraylist::findmax(){
//    current=head->nx;

//    int max=current->data;
//    while(current->nx!=NULL){
//        current=current->nx;
//        if(current->data>max)
//            max=current->data;
//    }
//    return max;
//}

//int arraylist::nodenumber(){
//    current=head->nx;
//    int counter=0;
//    while(current!=NULL){
//        current=current->nx;
//        counter++;
//    }
//    return counter;
//}

arraylist::arraylist(){
    head=tail=current=NULL;
    length_input();
}

void arraylist::length_input(){
    cout<<"please input the length of arraylist:";
    cin>>length;
        if(length){
            creatlist(length);
        }
        else{
            length_input();
        }
}


void arraylist::creatlist(int i){
    int counter=0;

    head=tail=new node;
    current=head;

    while(counter<i){
        tail=current=current->nx=new node;

        cout<<"\ninput the no."<<++counter<<" number: ";
        cin>>current->data;
    }

    tail->nx=NULL;
//    delete current;别在这贱兮兮的干这活！去掉了这个你试试！
}


arraylist::~arraylist(){
    while(head!=NULL){
        current=head;
        head=head->nx;
        delete current;
    }
    head=tail=NULL;
}


void arraylist::showlist(){
    current=head->nx;
    cout<<endl;

    while(current!=NULL){
        cout<<current->data<<"\t";
        current=current->nx;\
    }

    cout<<endl;
}


#endif // HEAD_H
